^
Replace previous command's string
TLDR
Run the previous command replacing string1 with string2
Remove string1 from the previous command
Replace string1 with string2 in the previous command and add string3 to its end
Replace all occurrences of string1
Print the substituted command without running it
SYNOPSIS
^string1^[string2][^][:modifiers]
PARAMETERS
string1
String to find in the previous command (first occurrence by default).
string2
Optional replacement string; omits to delete string1.
^ (third)
Optional trailing ^ to unambiguously end substitution.
:g
Global modifier: replaces all occurrences of string1.
:&
Repeat most recent substitution.
:h, :t, :r, :e
Word modifiers: head, tail, root, extension (after substitution).
DESCRIPTION
The ^ symbol in Linux Bash is a history expansion feature, not a traditional command. It enables quick editing of the previous command line by substituting specific strings. The basic syntax ^old^new^ searches the most recent command for the first occurrence of 'old' and replaces it with 'new', then executes the result.
This is ideal for fixing minor errors like typos in paths or arguments without retyping. For instance, after running grep error logfile.txt and realizing the wrong file, enter ^logfile.txt^access.log^ to execute grep error access.log.
Omitting the second string removes the first: ^old^^ deletes 'old'. Appending :g after the final ^ performs global replacement, affecting all occurrences.
History expansion activates in interactive shells when histexpand is enabled (default). It processes before execution, so use single quotes to escape if needed. This feature boosts efficiency for interactive use but requires caution to avoid unintended changes.
Full details in man bash under HISTORY EXPANSION.
CAVEATS
Interactive shells only; histexpand must be on. Unquoted strings expand immediately—use '' to prevent. Can execute unintended commands if strings match unexpectedly. Not script-safe without set +H.
EXAMPLES
ls /usr/bin/zip (fails)
^zip^gunzip^ → ls /usr/bin/gunzip
mv old.txt new.txt
^old^backup^:g → mv backup.txt new.txt (global)
echo hello world
^world^earth^^ → echo hello earth (deletes)
DISABLE
set +H turns off history expansion shell-wide.
HISTORY
Part of Bash since version 1.0 (1989), inspired by C-shell (csh) quick substitution. Evolved with modifiers for flexibility; POSIX sh lacks it, but Bash remains standard.


